﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamsPortal.DataAccess;

namespace TeamsPortal.ServiceClient
{
    public class MessagesServiceClient : IMessagesServiceClient
	{
        private MessagesDataAccess _da = new MessagesDataAccess();

        public Message Get(int messageId, int topicId)
        {
            return _da.Get(messageId, topicId);
        }

		public List<Message> GetByTopic(int topicId, int? userId, int pageSize, int pageNumber, out int totalMessages)
		{
            return _da.GetByTopic(topicId, userId, pageSize, pageNumber, out totalMessages);
		}

		/// <summary>
		/// Gets top latest message of a topic
		/// </summary>
		public List<Message> GetByTopicLatest(int topicId)
		{
			return _da.GetByTopicLatest(topicId);
		}

		/// <summary>
		/// Gets a list of messages from firstMsg to lastMsg
		/// </summary>
		public List<Message> GetByTopic(int topicId, int firstMsg, int lastMsg)
		{
			return _da.GetByTopic(topicId, firstMsg, lastMsg);
		}


		/// <summary>
		/// Gets an specific amount of messages starting from firstMsg
		/// </summary>
		/// <param name="topicId"></param>
		/// <param name="firstMsg"></param>
		/// <param name="amount"></param>
		/// <param name="initIndex"></param>
		/// <returns></returns>
		public List<Message> GetByTopicFrom(int topicId, int firstMsg, int amount)
		{
			return _da.GetByTopicFrom(topicId, firstMsg, amount);
		}

		/// <summary>
		/// Adds a new message to the topic
		/// </summary>
		/// <exception cref="ValidationException">If the model is not valid</exception>
		public void Add(Message message, string ip)
		{
			message.ValidateFields();
			_da.Add(message, ip);
		}

        public void UpdateBody(Message message, int editorUserId)
        {
            message.ValidateFields();
            _da.UpdateBody(message, message.Topic.Id, editorUserId);
        }

		public void Delete(int topicId, int messageId, int userId)
		{
			_da.Delete(topicId, messageId, userId);
		}

		/// <summary>
		/// Flags / Creates a mark on a message of a topic. The ip of flagger is stored.
		/// </summary>
		/// <param name="ip">Ip of the user creating the flag</param>
		public bool Flag(int topicId, int messageId, string ip)
		{
			return _da.Flag(topicId, messageId, ip);
		}

		/// <summary>
		/// Gets a list of flagged messages grouped by topic
		/// </summary>
		public List<Topic> ListFlagged()
		{
			return _da.ListFlagged();
		}

		public bool ClearFlags(int topicId, int messageId)
		{
			return _da.ClearFlags(topicId, messageId);
		}

        public bool IsReplied(int msgId, int topicId)
        {
            return _da.IsReplied(msgId, topicId);
        }
    }
}
